package com.hermit.system.controller;

import com.hermit.system.entity.MajorInfoEntity;
import com.hermit.system.service.MajorInfoService;
import com.hermit.util.ReqResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 控制层
*/
@RestController
@RequestMapping("api/system/majorInfo")
public class MajorInfoController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MajorInfoService majorInfoService;

    /**
    * 查询所有
    */
    @RequestMapping("/query-all")
    public ReqResult queryAll() {
        logger.info(" MajorInfo 查询所有数据");
        List<MajorInfoEntity> list = majorInfoService.queryAll();
        return ReqResult.ok().put("list", list);
    }

    /**
    * 新增
    */
    @RequestMapping("/save")
    public ReqResult save(@RequestBody MajorInfoEntity majorInfoEntity) {
        if (majorInfoEntity.getId() == null) {
            logger.info(" MajorInfo 保存信息");
            majorInfoEntity.setCreatedAt(new Date());
            majorInfoEntity.setUpdatedAt(new Date());
            majorInfoEntity.setDeleteFlag(0);
            majorInfoService.save(majorInfoEntity);
        } else {
            logger.info(" MajorInfo 修改信息");
            majorInfoEntity.setUpdatedAt(new Date());
            majorInfoService.updateById(majorInfoEntity);
        }
        return ReqResult.ok();
    }

    /**
    * 删除
    */
    @RequestMapping("/delete")
    public ReqResult delete(@RequestBody Map<String, Object> params) {
        logger.info("客户表 根据id删除");
        int id = (Integer) params.get("id");
        majorInfoService.delete(id);
        return ReqResult.ok();
    }
}